using micromsg;
using MicroMsg.Common.Utils;
using MicroMsg.Network;
using MicroMsg.Protocol;
using System;
namespace MicroMsg.Scene.Video
{
	public class NetSceneUploadVideo : NetSceneBaseEx<UploadVideoRequest, UploadVideoResponse, UploadVideoRequest.Builder>
	{
		private const string TAG = "NetSceneUploadVideo";
		private const int MM_VIDEO_NETWORK_WIFI = 1;
		private const int MM_VIDEO_NETWORK_OTHER = 2;
		private const int MM_VIDEO_CAMERA_FRONT = 1;
		private const int MM_VIDEO_CAMERA_REAR = 2;
		public UploadVideoContext mVideoContext;
		public void doScene(UploadVideoContext videoContext)
		{
			if (videoContext.mStatus != 0 && videoContext.mStatus != 4)
			{
				Log.smethod_1("NetSceneUploadVideo", "doScene videoContext.mStatus error, status = " + videoContext.mStatus);
				return;
			}
			videoContext.mStatus = 1;
			this.mVideoContext = videoContext;
			this.mVideoContext.mScene = this;
			this.doSceneEx();
		}
		private void doSceneEx()
		{
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mBuilder.FromUserName = this.mVideoContext.mVideoTrans.strFromUserName;
			this.mBuilder.ToUserName = this.mVideoContext.mVideoTrans.strToUserName;
			this.mBuilder.ClientMsgId = this.mVideoContext.mVideoTrans.strClientMsgId;
			this.mBuilder.PlayLength = (uint)this.mVideoContext.mVideoTrans.nDuration;
			this.mBuilder.VideoTotalLen = (uint)this.mVideoContext.mTotalLength;
			this.mBuilder.ThumbTotalLen = (uint)this.mVideoContext.mThumbTotalLength;
			this.mBuilder.NetworkEnv = (NetworkDeviceWatcher.isCurrentWifi() ? 1u : 2u);
			this.mBuilder.CameraType = 2u;
			this.mSessionPack.mCmdUri = "/cgi-bin/micromsg-bin/uploadvideo";
			this.mSessionPack.mConnectMode = 2;
			byte[] inBytes = new byte[0];
			byte[] array = this.mVideoContext.getRemainThumbToSend();
			if (array != null)
			{
				this.mVideoContext.mStatus = 2;
				this.mVideoContext.mSendingLength = array.Length;
				this.mBuilder.ThumbStartPos = (uint)this.mVideoContext.mThumbNetOffset;
				this.mBuilder.ThumbData = Util.toSKBuffer(array);
				this.mBuilder.VideoStartPos = (uint)this.mVideoContext.mNetOffset;
				this.mBuilder.VideoData = Util.toSKBuffer(inBytes);
				base.endBuilder();
				return;
			}
			array = this.mVideoContext.getRemainVideoToSend();
			if (array != null)
			{
				this.mVideoContext.mStatus = 3;
				this.mVideoContext.mSendingLength = array.Length;
				this.mBuilder.ThumbStartPos = (uint)this.mVideoContext.mThumbNetOffset;
				this.mBuilder.ThumbData = Util.toSKBuffer(inBytes);
				this.mBuilder.VideoStartPos = (uint)this.mVideoContext.mNetOffset;
				this.mBuilder.VideoData = Util.toSKBuffer(array);
				base.endBuilder();
				return;
			}
			this.mVideoContext.mStatus = 5;
			this.doSceneFinished();
		}
		protected override void onFailed(UploadVideoRequest request, UploadVideoResponse response)
		{
			if (this.mVideoContext == null)
			{
				return;
			}
			this.mVideoContext.mStatus = 4;
			this.doSceneFinished();
		}
		protected override void onSuccess(UploadVideoRequest request, UploadVideoResponse response)
		{
			if (this.mVideoContext == null)
			{
				return;
			}
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			if (ret != RetConst.MM_OK)
			{
				Log.smethod_1("NetSceneUploadVideo", "upload video failed, ret = " + ret);
				this.mVideoContext.mStatus = 4;
				this.doSceneFinished();
				return;
			}
			Log.smethod_4("NetSceneUploadVideo", string.Concat(new object[]
			{
				"upload block success , thumb_startpos = ",
				response.ThumbStartPos,
				", video_startpos = ",
				response.VideoStartPos,
				", thumb_translen = ",
				this.mVideoContext.mThumbNetOffset,
				", video_tranLen =",
				this.mVideoContext.mNetOffset
			}));
			if (this.mVideoContext.mStatus == 2)
			{
				this.mVideoContext.mThumbNetOffset += this.mVideoContext.mSendingLength;
			}
			else
			{
				if (this.mVideoContext.mStatus != 3)
				{
					return;
				}
				this.mVideoContext.mNetOffset += this.mVideoContext.mSendingLength;
			}
			this.mVideoContext.mVideoTrans.nMsgSvrID = (int)response.MsgId;
			if (this.mVideoContext.isUploadCompleted())
			{
				Log.smethod_4("NetSceneUploadVideo", "send Completed. ");
				this.mVideoContext.mStatus = 5;
				this.doSceneFinished();
				return;
			}
			this.mVideoContext.updateContext();
			this.mVideoContext.updateProgressInfo(0);
			this.doSceneEx();
		}
		public void doSceneFinished()
		{
			if (this.mVideoContext != null)
			{
				this.mVideoContext.updateProgressInfo(0);
				this.mVideoContext.updateContext();
				this.mVideoContext.onFinished();
				this.mVideoContext.mScene = null;
				this.mVideoContext = null;
				Log.smethod_5("NetSceneUploadVideo", "scene Finished. ");
			}
		}
		public void doCancel()
		{
			if (this.mVideoContext != null)
			{
				base.cancel();
				this.mVideoContext.mStatus = 4;
				this.doSceneFinished();
			}
		}
	}
}
